package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.StudentGradeHistory;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 学生所在班级与课程（分班表） 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-05
 */
@Service
public class StudentGradeHistoryServiceImpl extends ServiceImpl<StudentGradeHistoryMapper, StudentGradeHistory> implements IStudentGradeHistoryService {

    @Autowired
    @Lazy
    StudentGradeMapper studentGradeMapper;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    StudentGradeHistoryMapper studentGradeHistoryMapper;


    /**
     * 添加到分班历史表
     * @param yearPart
     * @param quarterNum
     * @param schoolId【可选】
     * @param subjectsId【可选】
     */
    @Override
    public void addStudentGradeHistory(Integer yearPart, Integer quarterNum, Long schoolId, Long subjectsId){
        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (yearPart!=null && quarterNum!=null){
            quarterNow.setYearPart(yearPart);
            quarterNow.setNum(quarterNum);
        }

        LocalDate dateNow = LocalDate.now();

        Integer pageSize =100;
        IPage<StudentGrade> iPage = new Page<>(1,pageSize);
        studentGradeMapper.getStudentGradeLastList(iPage,quarterNow.getYearPart(),quarterNow.getNum(),schoolId,subjectsId);

        Long allPage = iPage.getPages();
        for (Integer i=1;i<=allPage;i++){
            if (!i.equals(1)){
                iPage = new Page<>(i,pageSize);
                studentGradeMapper.getStudentGradeLastList(iPage,quarterNow.getYearPart(),quarterNow.getNum(),schoolId,subjectsId);
            }
            for (StudentGrade studentGrade:iPage.getRecords()){
                StudentGradeHistory studentGradeHistory = studentGradeHistoryMapper.selectStudentGradeHistoryMaxOneByStudent(studentGrade.getYearPart(),studentGrade.getQuarterNum(),studentGrade.getSubjectsId(),studentGrade.getStudentId(),studentGrade.getLessonType());
                if (studentGradeHistory == null){
                    studentGradeHistory = new StudentGradeHistory();
                    BeanUtils.copyProperties(studentGrade,studentGradeHistory);
                    studentGrade.setId(null);
                    studentGradeHistory.setStartDate(dateNow);
                    studentGradeHistory.setEndDate(LocalDate.parse("2099-12-31"));
                    studentGradeHistoryMapper.insert(studentGradeHistory);
                }else {
                    //不在原来的班
                    if (!studentGrade.getGradeId().equals(studentGradeHistory.getGradeId())){
                        LocalDate endDate = dateNow.minusDays(2);//由于是晨后执行因为结束时间在2天前
                        if (endDate.isBefore(studentGradeHistory.getStartDate())){
                            studentGradeHistoryMapper.deleteById(studentGradeHistory);
                        }else {
                            studentGradeHistory.setEndDate(endDate);
                            studentGradeHistoryMapper.updateById(studentGradeHistory);//结束原来的
                        }

                        //添加新的一条数据
                        BeanUtils.copyProperties(studentGrade,studentGradeHistory);
                        studentGradeHistory.setStartDate(dateNow.minusDays(1));//由于是晨后执行因为结束时间在1天前
                        studentGradeHistory.setEndDate(LocalDate.parse("2099-12-31"));
                        studentGradeHistoryMapper.insert(studentGradeHistory);
                    }
                }
            }
        }
    }
}
